package club.terroir.server.folder;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import club.terroir.server.util.UtilPersistence;

public class Folder_SSFactory {

	static Folder_SSFactory instance = new Folder_SSFactory();

	/**
	 * @return the instance
	 */
	public static Folder_SSFactory getInstance() {
		return instance;
	}

	public void persists(Folder_SS f) {
		EntityManager em = UtilPersistence.get().createEntityManager();
		try {
			em.persist(f);
		} finally {
			em.close();
		}
	}

	public Folder_SS getFolder_SS_ROOT(String idUser) {
		EntityManager em = UtilPersistence.get().createEntityManager();
		try {
			Query q = em.createQuery("select from " + Folder_SS.class.getName()
					+ " where idOwner=:1");
			q.setParameter(1, idUser);

			List<Folder_SS> listU = q.getResultList();
			if (listU.size() == 0) {
				return null;
			}
			return listU.get(0);
		} catch(Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			em.close();
		}
	}

	public Folder_SS getFolderRoot(String idUser) {
		EntityManager em = UtilPersistence.get().createEntityManager();
		try {
			//Ne marche pas Query q = em.createQuery("select from "	+ Folder_SS.class.getName() + " where isRoot=:1 && idOwner=:2");
			Query q = em.createQuery("select from "	+ Folder_SS.class.getName() + " where isRoot=:1 AND idOwner=:2");
			//Query q = em.createQuery("select from "	+ Folder_SS.class.getName() + " where isRoot=:1");
			q.setParameter(1, Boolean.TRUE);
			q.setParameter(2, idUser);
			@SuppressWarnings("unchecked")
			List<Folder_SS>  listU =  q.getResultList();
			System.out.println("Folder_SSFactory getFolderRoot idUser: "+idUser+"  listU.size "+listU.size());
			if (listU.size()==0){
				return null;
			}
			return listU.get(0);
		} catch(Exception e){
			e.printStackTrace();
			return null;
		} finally {
			em.close();
		}
	}	
	
	public String testInsertFolder() {
		String comment = "testInsertFolder";
		Folder_SS f = new Folder_SS();
		String idOwner = "a@a";
		f.setIdOwner(idOwner);
		f.setName("Test Folder");
		Folder_SS fChikd = new Folder_SS();
		fChikd.setName("Child");
		f.getListFolderChildreen().add(fChikd);
		EntityManager em1 = UtilPersistence.get().createEntityManager();
		em1.persist(f);
		// em1.flush();
		em1.close();
		comment += " insert done";
		Folder_SS fSS = getFolder_SS_ROOT(idOwner);
		comment += " fss :" + fSS;
		return comment;
	}

	public List<Folder_SS> getListFolder(EntityManager em) {
		// Query q = em.createQuery("select from " + Folder_SS.class.getName() +
		// " where idOwner=:1");
		Query q = em.createQuery("select from " + Folder_SS.class.getName());
		q.setHint("datanucleus.appengine.datastoreReadConsistency", "STRONG");
		// q.setParameter(1, idUser);
		@SuppressWarnings("unchecked")
		List<Folder_SS> listU = q.getResultList();
		return listU;

	}

	public String testInsertFolderList() {
		String comment = "TestKeyList";
		EntityManager em2 = UtilPersistence.get().createEntityManager();
		List<Folder_SS> list = getListFolder(em2);
		comment += "<br/> list Entity :" + list;
		comment += "<br/> list Entity list.size :" + list.size();
		em2.close();
		return comment;
	}
}
